function install(Vue) {
  const imageIsExist = function (url) {
    return new Promise((resolve) => {
      let img = new Image()
      img.onload = function () {
        resolve(this.complete)
        img = null
      }
      img.onerror = function () {
        resolve(false)
        img = null
      }
      img.src = url
    })
  }
  Vue.directive('default-img', async function (el, binding) {
    const imgURL = el.src
    const defaultURL = binding.value
    const exist = await imageIsExist(imgURL)
    el.setAttribute('src', exist ? imgURL : defaultURL)
  })
}

export default { install }
